﻿using System.Web.Mvc;

namespace CodeContrib.Web.Mvc
{
	// http://elegantcode.com/2009/11/07/asp-net-mvc-javascriptview/
	public abstract class JavaScriptController : Controller
	{
		protected virtual ViewResult JavaScriptView()
		{
			return JavaScriptView(null /* viewName */, null /* masterName */, null /* model */);
		}

		protected virtual ViewResult JavaScriptView(object model)
		{
			return JavaScriptView(null /* viewName */, null /* masterName */, model);
		}

		protected virtual ViewResult JavaScriptView(string viewName)
		{
			return JavaScriptView(viewName, null /* masterName */, null /* model */);
		}

		protected virtual ViewResult JavaScriptView(string viewName, string masterName)
		{
			return JavaScriptView(viewName, masterName, null /* model */);
		}

		protected virtual ViewResult JavaScriptView(string viewName, object model)
		{
			return JavaScriptView(viewName, null /* masterName */, model);
		}

		protected virtual ViewResult JavaScriptView(string viewName, string masterName, object model)
		{
			if (model != null)
			{
				ViewData.Model = model;
			}

			return new JavaScriptViewResult
				       {
					       ViewName = viewName,
					       MasterName = masterName,
					       ViewData = ViewData,
					       TempData = TempData
				       };
		}

		protected virtual ViewResult JavaScriptView(IView view)
		{
			return JavaScriptView(view, null /* model */);
		}

		protected virtual ViewResult JavaScriptView(IView view, object model)
		{
			if (model != null)
			{
				ViewData.Model = model;
			}

			return new JavaScriptViewResult
				       {
					       View = view,
					       ViewData = ViewData,
					       TempData = TempData
				       };
		}
	}
}